<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表单名称" prop="formName">
        <el-input
          v-model="queryParams.formName"
          placeholder="请输入表单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表单代码" prop="formCode">
        <el-input
          v-model="queryParams.formCode"
          placeholder="请输入表单代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"

          @click="handleAdd"
          v-hasPermi="['archive:formManage:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"

          @click="handleSync"
          v-hasPermi="['archive:formManage:sync']"
        >同步</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"

          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['archive:formManage:edit']"
        >设置代码</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"

          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive:formManage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"

          @click="handleExport"
          v-hasPermi="['archive:formManage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formManageList" @selection-change="handleSelectionChange" :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="formId" />
      <el-table-column label="表单名称" align="center" prop="formName" />
      <el-table-column label="表单代码" align="center" prop="formCode" />
      <!--<el-table-column label="是否启用" align="center" prop="isEnable" />-->
      <el-table-column label="是否启用">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isEnable" align="center" active-value="1" inactive-value="0"
                     @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否档案部门分单" width="200">
        <template slot-scope="scope" >
          <el-switch  v-hasPermi="['archive:formManage:edit']" v-model="scope.row.isSplit" align="center" active-value="1" inactive-value="0"
                     @change="handleSplitChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <!-- <el-button

            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive:formManage:edit']"
          >修改</el-button>-->
          <!--<el-button

            type="text"
            icon="el-icon-edit"
            @click="handleTableDetail(scope.row)"
            v-hasPermi="['archive:formManage:edit']"
          >表单详情</el-button>-->
          <el-button

            type="text"
            icon="el-icon-edit"
            @click="handleTableRule(scope.row)"
            v-hasPermi="['archive:formManage:edit']"
          >表单详情</el-button>
          <el-button

            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive:formManage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改表单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="表单名称" prop="formName">
          <el-input v-model="form.formName" placeholder="请输入表单名称" />
        </el-form-item>-->
        <el-form-item label="表单代码" prop="formCode">
          <el-input v-model="form.formCode" placeholder="请输入表单代码" />
        </el-form-item>
        <!--<el-form-item label="是否启用" prop="isEnable">
          <el-input v-model="form.isEnable" placeholder="请输入是否启用" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

        <kl  :openSelect="openSelect" :detailFormCode = "formCode" @handleClose="handleClose"></kl>
    <RULE  :openSelectRule="openSelectRule" @handleClose="handleCloseRule" ref="ruleref"></RULE>
  </div>
</template>

<script>
import {
  addFormManage,
  delFormManage,
  getFormManage,
  listFormManage,
  syncFormManage,
  updateFormManage
} from "@/api/archive/formManage";
// import {selectDetail} from './formManageDetail/selectDetail'
import kl from "@/components/kl/index.vue"
import {debounce} from '@/utils'
import RULE from "@/components/RULE/index.vue"
import {listFormManageone} from "../../../api/archive/formManage";

export default {
    name: "FormManage",
    components:{kl,RULE},
    data() {
      return {
        openSelect:false,

        formCode:"",

        //表单id
        detailId: "",

        openSelectRule:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表单管理表格数据
        formManageList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          formName: null,
          formCode: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        tableHeight:500,// 定义table高度
      };
    },
    created() {
      this.clientHeight = document.documentElement.clientHeight;
      this.tableHeight = this.clientHeight - 286;
      this.getList();
    },
    watch: {
      //  监听高度变化
      clientHeight(newValue, oldValue) {
        if(newValue) {
          this.getTableHeight()
        }
      }
    },
    mounted(){
      window.openUserDlg = this.openUserDlg;
      window.onresize = () => {
        this.clientHeight = document.documentElement.clientHeight;
        this.tableHeight = this.clientHeight - 286;
      }
    },
    
    methods: {
      /** 查询表单管理列表 */
      getList() {
        this.loading = true;
        listFormManage(this.queryParams).then(response => {
          this.formManageList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 防抖
      getTableHeight: debounce(function() {
        // 给table高度赋值
        this.tableHeight = this.clientHeight * 0.6 ;
      }, 300),
      // 表单重置
      reset() {
        this.form = {
          formId: null,
          formName: null,
          formCode: null,
          isEnable: null
        };
        this.resetForm("form");
      },

      handleClose(value){
        this.openSelect=value
      },
      handleCloseRule(value){
        this.openSelectRule=value
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.formId)
        this.single = selection.length!==1
        this.multiple = !selection.length
        this.label = selection.map(item => item.formName)
      },
      // 立档单位信息状态修改
      handleStatusChange(row) {
        console.log(row)
        let text = row.isEnable === "1" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '" "' + row.formName + '"分单吗？').then(function () {
          return updateFormManage({ formId: row.formId, isEnable: row.isEnable });
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.isEnable = row.isEnable === "1" ? "0" : "1";
        });
      },
      // 立档单位信息状态修改
      handleSplitChange(row) {
        console.log(row)
        let text = row.isSplit === "1" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '" "' + row.formName + '"吗？').then(function () {
          return updateFormManage({ formId: row.formId, isSplit: row.isSplit });
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.isSplit = row.isSplit === "1" ? "0" : "1";
        });
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加表单管理";
      },
      handleSync() {
        syncFormManage(this.form).then(response => {
          this.$modal.msgSuccess("同步成功，已同步"+response.data+"条数据！");
          this.open = false;
          this.getList();
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const formId = row.formId || this.ids
        getFormManage(formId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "设置代码";
        });
      },
      /** 修改按钮操作 */
      handleTableDetail(row) {
        if (row.formCode==null){
         this.$modal.msgWarning("请先设置表单代码");
         return;
        }
        this.formCode = row.id;
        this.openSelect = true;

      },/** 修改按钮操作 */
      handleTableRule(row) {
        if (row.formCode==null){
          this.$modal.msgWarning("请先设置表单代码");
          return;
        }
        this.detailId = row.formId;
        console.log('123234',this.detailId)
        this.$nextTick(() => {   //此函数执行时所有的DOM挂载和渲染都已完成
          console.log(this.detailId)
          this.$refs.ruleref.init(this.detailId);     //获取子组件中init方法并将父组件id传递给子组件
        });
        this.openSelectRule = true;

      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let queryParamThis= {
              pageNum:1,
              pageSize:99999,
              formName: null,
              formCode: this.form.formCode,
              formId: this.form.formId
            };
            listFormManageone(queryParamThis).then(response => {
              if (response.total == 0){
                if (this.form.formId != null) {
                  updateFormManage(this.form).then(response => {
                    this.$modal.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                  });
                } else {
                  addFormManage(this.form).then(response => {
                    this.$modal.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                  });
                }
              }else {
                this.$modal.msgWarning("表单代码已存在！");
              }
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const formIds = row.formId || this.ids;
        const label = row.formName || this.label;
        this.$modal.confirm('是否确认删除表单名称为"' + label + '"的数据项？').then(function() {
          return delFormManage(formIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('archive/formManage/export', {
          ...this.queryParams
        }, `formManage_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
